/*
 * main.c
 *
 *  Created on: 03/08/2012
 *      Author: marcio
 */

#include "ParallelizableOperation.h"
#include "BitTools.h"
#include "ArrayvEBTree.h"
#include "OrderedSet.h"
#include "OrderedBinaryRelation.h"
#include "ArrayOrderedSet.h"
#include "Graph.h"
#include "Graphs.h"
#include "WGraphs.h"
#include "vEBTree.h"
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>

long* sd;
long* d;

int comp(const void* a, const void* b){
	int ia = *(int*)a;
	int ib = *(int*)b;
	if(sd[ia] != sd[ib])return sd[ia]-sd[ib];
	else return d[ia] - d[ib];
}

void generate(double prob, ArrayvEBTree<int>* a){
	for(int i = 0; i < a->size(); i++){
		 if(rand()%101 <= prob)a->toSet(i);
	}
}

int main(int argc, char *argv[]){
	ArrayvEBTreeFactory<int>* factory = new ArrayvEBTreeFactory<int>();
	ArrayvEBTree<int>* array = new ArrayvEBTree<int>(1000,2);
	ArrayvEBTree<int>::Iterator* it = (ArrayvEBTree<int>::Iterator*)array->newSetIterator();
	generate(1, array);
	array->show();
	it->start();
	while(it->hasNext())printf("next = %lu \n", it->next());

	printf("next de %d é %lu \n", 1, array->nextSetElement(1));
	printf("next de %d é %lu \n", 5, array->nextSetElement(5));
	printf("next de %d é %lu \n", 16, array->nextSetElement(16));
	printf("next de %d é %lu \n", 31, array->nextSetElement(31));
	printf("next de %d é %lu \n", 50, array->nextSetElement(50));
	printf("next de %d é %lu \n", 100, array->nextSetElement(100));

	Graph<ArrayOrderedSet<int> >* g = NULL;
	FILE* graphfile = fopen(argv[1],"a+");
	ArrayOrderedSetFactory<int>* factory1 = new ArrayOrderedSetFactory<int>();
	long altura = 1;
	long n = Graphs<ArrayOrderedSet<int> , int>::readDimacsGraph(&g, graphfile, GT_GRAPH, factory1, (void*)&altura);

	/*long st[n];
	long dg[n];
	long R[n];
	long colors[n];
	long co = 0;
	long v;
	long u;
	long i = 0;
	ArrayOrderedSet<int>* avaiblecolors = new ArrayOrderedSet<int>(n);
	ArrayOrderedSet<int>* set = new ArrayOrderedSet<int>(n);
	OrderedSet<long>::Iterator* it;
	g->show();

	for(long j = 0; j < n; j++){
		st[j] = 0;
		dg[j] = g->degree(j);
		R[j] = j;
		colors[i] = 0;
	}
	sd = st;
	d = dg;

	qsort(R, n, sizeof(long), comp);

	for(long i = 0; i < n ; i++)printf("%lu -", R[i]);
	printf("\n");

	while(i < n){
		v = R[i++];
		set->toSet(v);
		it = g->neig(v)->newSetIterator();
		it->start();
		avaiblecolors->allToSet();
		avaiblecolors->toCompl(0);
		printf(" visitando = %lu \n", v);
		while(it->hasNext()){
			u = it->next();
			if(set->isInSet(u)){
				printf("1 - %lu \n", u);
				getchar();
				avaiblecolors->toCompl(colors[u]);
			}else{
				printf("2 - %lu \n", u);
				getchar();
				st[u]++;
			}
		}
		colors[v] = avaiblecolors->firstInSet();
		printf("cor atribuida = %lu \n", colors[v]);
		getchar();
		if(colors[v] > co) co = colors[v];
		qsort(&R[i], n-i, sizeof(long), comp);
	}

	printf("Used %lu colors \n", co);

	for(long i = 0; i < n; i++)printf("color[%lu] = %lu \n", i, colors[i]);*/

 	return 0;
}

